package com.gonsin.gview.common.auth;

import com.gonsin.base.exceptions.IResultCode;

import java.util.*;
import java.util.stream.Collectors;

public enum BaseResultCodes implements IResultCode {

    OK(200, "成功"),
    EXIST(300, "重复添加或者注册"),
    USER_CODE_ERROR(301, "验证码错误"),
    USER_TIMEOUT(302, "登录超时"),
    PASSWORD_WRONG(303, "登录错误"),
    WRONG_DATA(304, "数据错误"),
    NOT_EMPTY(305, "数据非空，不能删除"),
    CODE_TIMEOUT(306, "验证码超时"),
    STATE_WRONG(307, "状态错误"),
    INPUT_WRONG(308, "用户输入有误"),
    INPUT_EXCEEDED(309, "操作过多"),
    NOT_ALLOW(310, "权限不足"),
    NOT_BINDING(311, "未绑定"),
    BUSY(312, "对象忙碌"),
    ACTION_TIMEOUT(313, "操作超时"),
    PASSWORD_NOT_SAME(315, "密码和确认密码不相同"),
    PARAM_ERROR(317, "参数错误"),


    NOT_FOUND(404, "未找到相应资源"),
    TOO_FAST(405, "操作过快"),
    NOT_SUPPORT(501, "不支持该功能"),
    FUNCTION_ERROR(502, "功能错误"),
    KEY_ERROR(503, "传递的key错误"),
    INTERFACE_VALIDATE(505, "该接口已无效"),
    ENUMS_ERROR(506, "枚举值错误"),
    NEWS_PARAMETER_ERROR(507, "发送News出错"),
    SEARCH_EMPTY(508, "searchPo其中的变量为空"),
    LOG_NOT_FOUND(509, "根据logKey找不到数据"),
    PROJECT_NOT_FOUND(510, "根据projectKey找不到数据"),
    ADVICE_NOT_FOUND(511, "根据adviceKey找不到数据"),

    ;

    private int value;

    private String name;

    BaseResultCodes(int mode, String name) {
        this.value = mode;
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    private static Map<Integer, BaseResultCodes> paramKeysMap;


    private static List<Map<String, Object>> allResultCodes = new ArrayList<>();
    static {
        paramKeysMap = new HashMap<>();
        BaseResultCodes[] keys = BaseResultCodes.values();
        for (BaseResultCodes key : keys) {
            paramKeysMap.put(key.getValue(), key);
        }


        allResultCodes.addAll(Arrays.stream(values()).map(a -> {
            Map<String, Object> m = new HashMap<>();
            m.put("value", a.getValue());
            m.put("name", a.getName());
            return m;
        }).collect(Collectors.toList()));
    }

    public static BaseResultCodes get(int code) {
        return paramKeysMap.get(code);
    }

    /**
     * 其他模块通过该方法将自身的枚举值传递给BaseResultCode
     * @param otherCodes
     */
    public static void add(IResultCode[] otherCodes){
        allResultCodes.addAll(Arrays.stream(otherCodes).map(a -> {
            Map<String, Object> m = new HashMap<>();
            m.put("value", a.getValue());
            m.put("name", a.getName());
            return m;
        }).collect(Collectors.toList()));
    }


    /**
     * 获取列表
     * @return
     */
    public static List<Map<String, Object>> list(){
        return allResultCodes;
    }
}
